package com.labichaoka.chaoka.ui.baseinfo.person_pip;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseActivity;
import com.labichaoka.chaoka.base.GlobalParams;
import com.labichaoka.chaoka.entity.SaveInformationRequest;
import com.labichaoka.chaoka.ui.baseinfo.person.PersonInfoAdapter;
import com.labichaoka.chaoka.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DatePicker;

import static com.labichaoka.chaoka.R.id.graduate_txt;

/*
* 个人信息-种子用户
* */

public class PersonInfoPipActivity extends BaseActivity implements PersonInfoPipView{

    @BindView(R.id.income_source)
    TextView incomeSource;
    @BindView(R.id.next1)
    TextView next1;
    @BindView(R.id.income_month)
    TextView incomeMonth;
    @BindView(R.id.next2)
    TextView next2;
    @BindView(R.id.loan_selected_img)
    ImageView loanSelectedImg;
    @BindView(R.id.loan_unselected_img)
    ImageView loanUnselectedImg;
    @BindView(R.id.edu)
    TextView edu;
    @BindView(R.id.next3)
    TextView next3;
    @BindView(graduate_txt)
    TextView graduateTxt;
    @BindView(R.id.next4)
    TextView next4;
    @BindView(R.id.graduate_school)
    EditText graduateSchool;
    @BindView(R.id.submit)
    Button btn;

    private PersonInfoPipPresenter presenter;
    private Dialog dialog;
    private List<GlobalParams.Education> list;
    private TextView commonTv;

    private String uniRec, graduateData, uniName, hasWork, company, workTel, workAddrInfo, workAddrDetailInfo, expectAmount, incomeResource, monthlyIncome, hasLoan, cname1, cphone1, crelation1, cname2, cphone2, crelation2, liveAddrInfo, liveAddrDetailInfo, workProvinceCityDistrict, provinceCityDistrict;


    @Override
    protected int initLayoutId() {
        return R.layout.activity_person_info_pip;
    }

    @Override
    protected void initView() {
        next1.setTypeface(iconfont);
        next2.setTypeface(iconfont);
        next3.setTypeface(iconfont);
        next4.setTypeface(iconfont);
    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        dialog = new Dialog(mContext, R.style.Translucent_NoTitle);
        hasLoan = "0"; //默认无贷款
        presenter=new PersonInfoPipPresenterImpl(new PersonInfoPipInteractorImpl(),this);
    }

    @OnClick({R.id.edu_layout, R.id.income_layout, R.id.income_by_month_layout,
            R.id.graduate_time_layout, R.id.submit,
            R.id.loan_selected_layout, R.id.loan_unselected_layout
    })
    public void click(View v) {
        switch (v.getId()) {
            case R.id.edu_layout:
                commonTv = edu;
                initDialogData(0, "教育程度");
                break;
            case R.id.income_layout:
                commonTv = incomeSource;
                initDialogData(1, "收入来源");
                break;
            case R.id.income_by_month_layout:
                commonTv = incomeMonth;
                initDialogData(2, "月收入");
                break;
            case R.id.graduate_time_layout:
                onYearMonthPicker();
                break;

            case R.id.submit:
                saveInformation();
                break;

            case R.id.loan_selected_layout:
                loanSelectedImg.setImageResource(R.mipmap.work_selected);
                loanUnselectedImg.setImageResource(R.mipmap.work_unselected);
                hasLoan = "1";

                break;
            case R.id.loan_unselected_layout:
                loanSelectedImg.setImageResource(R.mipmap.work_unselected);
                loanUnselectedImg.setImageResource(R.mipmap.work_selected);
                hasLoan = "0";
                break;
        }

    }

    private void saveInformation() {
        SaveInformationRequest request = new SaveInformationRequest();
        request.setCname1(cname1);
        request.setCname2(cname2);
        request.setCompany(company);
        request.setCphone1(cphone1);
        request.setCrelation1(crelation1);
        request.setCphone2(cphone2);
        request.setCrelation2(crelation2);
        request.setExpectAmount(expectAmount);
        request.setGraduateData(graduateData);
        request.setHasLoan(hasLoan);
        request.setHasWork(hasWork);
        request.setIncomeResource(incomeResource);
        request.setLiveAddrDetailInfo(liveAddrDetailInfo);
        request.setLiveAddrInfo(liveAddrInfo);
        request.setMonthlyIncome(monthlyIncome);
        request.setProvinceCityDistrict(provinceCityDistrict);
        request.setUniName(uniName);
        request.setWorkAddrDetailInfo(workAddrDetailInfo);
        request.setWorkTel(workTel);
        request.setWorkAddrInfo(workAddrInfo);
        request.setWorkProvinceCityDistrict(workProvinceCityDistrict);
        request.setUniRec(uniRec);

        presenter.saveInformation(request);

    }

    public void onYearMonthPicker() {
        DatePicker picker = new DatePicker(this, DatePicker.YEAR_MONTH);
        picker.setWidth(picker.getScreenWidthPixels());
        picker.setRangeStart(2008, 1, 14);
        picker.setRangeEnd(2018, 12, 11);
        picker.setSelectedItem(2010, 1);
        picker.setTopLineColor(getResources().getColor(R.color.divide_f4f4f4));
        picker.setLabelTextColor(0x00000000);
        picker.setDividerColor(getResources().getColor(R.color.divide_f4f4f4));
        picker.setSubmitTextColor(getResources().getColor(R.color.mainColor));
        picker.setCancelTextColor(getResources().getColor(R.color.mainColor));
        picker.setTextSize(14);
        picker.setTitleText("请选择日期");
        picker.setTitleTextSize(15);
        picker.setTitleTextColor(getResources().getColor(R.color.mainColor));
        picker.setContentPadding(15,15);
        picker.setTextColor(getResources().getColor(R.color.mainColor),getResources().getColor(R.color.font_909090) );
        picker.setOnDatePickListener(new DatePicker.OnYearMonthPickListener() {
            @Override
            public void onDatePicked(String year, String month) {
                graduateData = year + "-" + month;
                graduateTxt.setText(year + "-" + month);
                graduateTxt.setTextColor(getResources().getColor(R.color.font_3a3a38));
                checkButtonState();
            }
        });
        picker.show();
    }

    public void showBottomDialog(int type, String titleStr) {
        final LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.person_info_dialog, null);
        dialog.setContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.showdialog);
        TextView close_icon = (TextView) v.findViewById(R.id.close);
        TextView title = (TextView) v.findViewById(R.id.title);
        RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.rv_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        //添加自定义分割线
        DividerItemDecoration divider = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this,R.drawable.divide_drawable));
        recyclerView.addItemDecoration(divider);

        title.setText(titleStr);
        close_icon.setTypeface(iconfont);
        close_icon.setOnClickListener((view) -> {
            dialog.dismiss();
        });


        PersonInfoAdapter adapter = new PersonInfoAdapter(mContext, list);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener((position -> {
            dialog.dismiss();
            setValue(type, position);
        }));

        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialog.onWindowAttributesChanged(wl);
        dialog.setCanceledOnTouchOutside(true);

        dialog.show();

    }

    public void initDialogData(int type, String titleStr) {
        switch (type) {
            case 0:
                list = GlobalParams.getEducation();
                break;
            case 1:
                list = GlobalParams.getIncome();
                break;
            case 2:
                list = GlobalParams.getIncomeByMonth();
                break;

        }

        showBottomDialog(type, titleStr);
    }

    public void setValue(int type, int pos) {
        //设置提交的value
        switch (type) {
            case 0:
                //教育程度
                uniRec = list.get(pos).getId();
                break;
            case 1:
                //收入来源
                incomeResource = list.get(pos).getId();
                break;
            case 2:
                //月收入
                monthlyIncome = list.get(pos).getId();
                break;
        }
        checkButtonState();

        commonTv.setText(list.get(pos).getValue());
        commonTv.setTextColor(getResources().getColor(R.color.font_3a3a38));
    }


    public void checkButtonState() {
        if (TextUtils.isEmpty(incomeResource) || TextUtils.isEmpty(monthlyIncome) || TextUtils.isEmpty(hasLoan)) {
            btn.setEnabled(false);
        } else {
            btn.setEnabled(true);
        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        graduateSchool.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                uniName = s.toString().trim();
                checkButtonState();
            }
        });
    }

    @Override
    public void showProgress() {
        showLoadingProgress();
    }

    @Override
    public void hideProgress() {
        hideLoadingProgress();
    }

    @Override
    public void showMessage(String str) {
        ToastUtils.show(str);
    }

    @Override
    public void toNext() {
        finish();
    }
}
